package com.business.dashboard.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.business.dashboard.entity.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 前端用户数据访问层
 * 
 * @author Claude
 * @since 2025-07-06
 */
@Mapper
public interface UserDao extends BaseMapper<User> {

    /**
     * 根据用户名查询用户
     */
    @Select("SELECT * FROM users WHERE username = #{username} AND deleted = 0")
    User selectByUsername(@Param("username") String username);

    /**
     * 根据邮箱查询用户
     */
    @Select("SELECT * FROM users WHERE email = #{email} AND deleted = 0")
    User selectByEmail(@Param("email") String email);

    /**
     * 根据手机号查询用户
     */
    @Select("SELECT * FROM users WHERE phone = #{phone} AND deleted = 0")
    User selectByPhone(@Param("phone") String phone);

    /**
     * 查询用户列表（分页）
     */
    List<User> selectUserList(@Param("offset") int offset, 
                             @Param("size") int size, 
                             @Param("keyword") String keyword,
                             @Param("industry") String industry,
                             @Param("location") String location);

    /**
     * 获取最近注册的用户
     */
    @Select("SELECT * FROM users WHERE deleted = 0 ORDER BY created_at DESC LIMIT #{limit}")
    List<User> selectRecentUsers(@Param("limit") int limit);

    /**
     * 搜索用户
     */
    @Select("SELECT * FROM users WHERE deleted = 0 AND (username LIKE CONCAT('%', #{keyword}, '%') OR email LIKE CONCAT('%', #{keyword}, '%') OR name LIKE CONCAT('%', #{keyword}, '%')) LIMIT #{offset}, #{size}")
    List<User> searchUsers(@Param("keyword") String keyword, @Param("offset") int offset, @Param("size") int size);

    /**
     * 根据认证状态统计用户数量
     */
    @Select("SELECT COUNT(*) FROM users WHERE verification_status = #{status} AND deleted = 0")
    long countByVerificationStatus(@Param("status") int status);

    /**
     * 根据状态统计用户数量
     */
    @Select("SELECT COUNT(*) FROM users WHERE status = #{status} AND deleted = 0")
    long countByStatus(@Param("status") int status);

    /**
     * 统计今日注册用户数量
     */
    @Select("SELECT COUNT(*) FROM users WHERE DATE(created_at) = CURDATE() AND deleted = 0")
    long countTodayRegistrations();

    /**
     * 统计今日登录用户数量
     */
    @Select("SELECT COUNT(*) FROM users WHERE DATE(last_login_time) = CURDATE() AND deleted = 0")
    long countTodayLogins();

}